package logic

import (
	"context"
	"fmt"
	"hui-hui/models"

	"hui-hui/hui_hui_rpc/hui_hui_rpc"
	"hui-hui/hui_hui_rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FindPlanLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFindPlanLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindPlanLogic {
	return &FindPlanLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FindPlanLogic) FindPlan(in *hui_hui_rpc.FindPlanRequest) (*hui_hui_rpc.FindPlanResponse, error) {
	// todo: add your logic here and delete this line
	var plan struct {
		models.Plan
		UserName string
	}
	if models.DB.Model(&models.Plan{}).Joins("join users on plans.user_id = users.id").Where("plans.id = ?", in.Id).Select("plans.*,users.name as user_name").Scan(&plan).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &hui_hui_rpc.FindPlanResponse{
		UserName:  plan.UserName,
		CreatedAt: plan.CreatedAt.String(),
		Title:     plan.Title,
		Image:     plan.Image,
		Des:       plan.Des,
	}, nil
}
